package com.doradance.app.mine.collect;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.doradance.R;
import com.doradance.account.Account;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.FragmentUtils;
import com.doradance.app.activity.NoNetFragment;
import com.doradance.app.activity.NullDataFragment;
import com.doradance.app.home.ClassEvent;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.FreeItemEntity;
import com.doradance.entity.NullCollectEntity;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.GsonUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class CollectActivity extends BaseActivity {
    private List<MyLiveList> mList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        hideToolbar();
        initData();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(NullCollectEntity refreshEvent) {
        Log.e("11111212", "11111111111");
        FragmentUtils.replaceFragment(getSupportFragmentManager(), R.id.fl_collect,new NullDataFragment());
    }

    private void initData() {
        MyHttpRequest request = new MyHttpRequest(this);
        UrlParams urlParams = new UrlParams();
        request.get(UrlCenter.COURSES_FAVORITE_LIST, urlParams, new DataRequestListener<List<FreeItemEntity>>(FreeItemEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(List<FreeItemEntity> o) {
                super.onSuccess(o);

                try{
                    for(int i = 0; i < o.size(); i++){
                        MyLiveList myLiveList = new MyLiveList();
                        myLiveList.setFreeItemEntity(o.get(i));
                        mList.add(myLiveList);
                    }
                    CollectFragment collectFragment = new CollectFragment(mList);
                    FragmentUtils.replaceFragment(getSupportFragmentManager(), R.id.fl_collect, collectFragment);
                }catch(Exception e){
                    FragmentUtils.replaceFragment(getSupportFragmentManager(), R.id.fl_collect,new NullDataFragment());
                }
            }



            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    public static void start(Activity activity){
        Intent intent = new Intent(activity, CollectActivity.class);
        activity.startActivity(intent);
    }
    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_collect,null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
